package com.njthird.busi.controller;

import java.math.BigDecimal;
import java.util.List;

import com.njthird.busi.domain.BusiCuscapitalflow;
import com.njthird.busi.service.IBusiCuscapitalflowService;
import com.njthird.common.utils.DateUtils;
import com.njthird.common.utils.SecurityUtils;
import com.njthird.common.utils.sign.Md5Utils;
import com.njthird.common.utils.uuid.IdUtils;
import com.njthird.common.utils.uuid.UUID;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.njthird.common.annotation.Log;
import com.njthird.common.core.controller.BaseController;
import com.njthird.common.core.domain.AjaxResult;
import com.njthird.common.enums.BusinessType;
import com.njthird.busi.domain.BusiCustomers;
import com.njthird.busi.service.IBusiCustomersService;
import com.njthird.common.utils.poi.ExcelUtil;
import com.njthird.common.core.page.TableDataInfo;

/**
 * 客户管理Controller
 * 
 * @author njthird
 * @date 2020-08-06
 */
@RestController
@RequestMapping("/busi/customers")
public class BusiCustomersController extends BaseController
{
    @Autowired
    private IBusiCustomersService busiCustomersService;

    @Autowired
    private IBusiCuscapitalflowService busiCuscapitalflowService;
    /**
     * 查询客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('busi:customers:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusiCustomers busiCustomers)
    {
        startPage();
        List<BusiCustomers> list = busiCustomersService.selectBusiCustomersList(busiCustomers);
        return getDataTable(list);
    }

    /**
     * 导出客户管理列表
     */
    @PreAuthorize("@ss.hasPermi('busi:customers:export')")
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(BusiCustomers busiCustomers)
    {
        List<BusiCustomers> list = busiCustomersService.selectBusiCustomersList(busiCustomers);
        ExcelUtil<BusiCustomers> util = new ExcelUtil<BusiCustomers>(BusiCustomers.class);
        return util.exportExcel(list, "customers");
    }

    /**
     * 获取客户管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('busi:customers:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(busiCustomersService.selectBusiCustomersById(id));
    }

    /**
     * 新增客户管理
     */
    @PreAuthorize("@ss.hasPermi('busi:customers:add')")
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BusiCustomers busiCustomers)
    {
        busiCustomers.setId(IdUtils.fastSimpleUUID());
        busiCustomers.setKhye(BigDecimal.valueOf(0.00));
        busiCustomers.setKhmm(Md5Utils.hash("012356"));
        busiCustomers.setCreateBy(SecurityUtils.getUsername());
        busiCustomers.setCreateTime(DateUtils.getNowDate());
        return toAjax(busiCustomersService.insertBusiCustomers(busiCustomers));
    }

    /**
     * 修改客户管理
     */
    @PreAuthorize("@ss.hasPermi('busi:customers:edit')")
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusiCustomers busiCustomers)
    {
        busiCustomers.setUpdateBy(SecurityUtils.getUsername());
        busiCustomers.setUpdateTime(DateUtils.getNowDate());
        return toAjax(busiCustomersService.updateBusiCustomers(busiCustomers));
    }
    /**
     * 修改客户管理资金
     */
    @PreAuthorize("@ss.hasPermi('busi:customers:edit')")
    @Log(title = "客户管理资金", businessType = BusinessType.UPDATE)
    @PostMapping("/chgamount")
    public AjaxResult chgamount(@RequestBody BusiCustomers busiCustomers)
    {
        BigDecimal khye = busiCustomers.getKhye();
        if(busiCustomers.getChgdirection().equals("increase"))
            khye = khye.add(busiCustomers.getChgamount());
        else
            khye = khye.subtract(busiCustomers.getChgamount());
        BusiCuscapitalflow cuscapitalflow = new BusiCuscapitalflow();
        cuscapitalflow.setId(IdUtils.fastSimpleUUID());
        cuscapitalflow.setFlowname("手动修改");
        cuscapitalflow.setCusid(busiCustomers.getId());
        cuscapitalflow.setRemark(busiCustomers.getChgremark());
        cuscapitalflow.setCapitaldirection(busiCustomers.getChgdirection());
        cuscapitalflow.setCreateBy(SecurityUtils.getUsername());
        cuscapitalflow.setCreateTime(DateUtils.getNowDate());
        cuscapitalflow.setBeforeamount(busiCustomers.getKhye());
        cuscapitalflow.setAmount(busiCustomers.getChgamount());
        cuscapitalflow.setAfteramount(khye);
        busiCuscapitalflowService.insertBusiCuscapitalflow(cuscapitalflow);
        busiCustomers.setKhye(khye);
        busiCustomers.setUpdateBy(SecurityUtils.getUsername());
        busiCustomers.setUpdateTime(DateUtils.getNowDate());
        return toAjax(busiCustomersService.updateBusiCustomers(busiCustomers));
    }
    /**
     * 重置客户密码
     */
    @PreAuthorize("@ss.hasPermi('busi:customers:edit')")
    @Log(title = "重置客户密码", businessType = BusinessType.UPDATE)
    @PostMapping("/resetpwd/{ids}")
    public AjaxResult resetpwd(@PathVariable String[] ids)
    {

        return toAjax(busiCustomersService.resetBusiCustomersByIds(ids));
    }

    /**
     * 删除客户管理
     */
    @PreAuthorize("@ss.hasPermi('busi:customers:remove')")
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(busiCustomersService.deleteBusiCustomersByIds(ids));
    }
}
